#include "MeshSceneNode.h"
#include "include/IDriver.h"
#include "include/IMeshBuffer.h"
#include "MeshBuffer.h"

namespace ISNW
{
namespace scene
{
	void MeshSceneNode::render()
	{
		if(isVisible)
		{
			if(mesh)
			{
				if(scenemanager)
				{
					u32 count = mesh->getmeshbuffercount();
					for(u32 i = 0; i < count; ++i)
					{
						IMeshBuffer* ibuffer = mesh->getmeshbuffer(i);
						MeshBuffer* buffer = static_cast<MeshBuffer*>(ibuffer);

						core::array<u16> index_buffer;
						for(u32 k = 0; k < buffer->indices_buffer.size(); ++k)
						{
							index_buffer.push_back((u16)(buffer->indices_buffer[k]));
						}

						scenemanager->getdriver()->setmaterial(buffer->m_material);

						scenemanager->getdriver()->drawtriangles(buffer->vertex_buffer.pointer(),video::STANDAND_VERTEX,buffer->vertex_buffer.size(),index_buffer.pointer(),buffer->indices_buffer.size()/3);

					}
				}
			}
		}
	}

	void MeshSceneNode::onRegisterSceneNode()
	{
		if(scenemanager)
		{
			scenemanager->registernodeforrendering(this,E_RT_SOLID);
		}

		ISceneNode::onRegisterSceneNode();
	}

} // end namespace scene
} // end namespace ISNW